//
//  DR_ResultSucceccView.swift
//  DocReaderRP
//
//  Created by  mac on 2025/11/18.
//

import UIKit
import SnapKit

class DR_ResultSucceccView: UIView {
    
    var onOpenTapped: (() -> Void)?
    var onShareTapped: (() -> Void)?
    
    // MARK: - UI Components
    
    // 顶部成功区域
    private let successIconView: UIImageView = {
        let v = UIImageView()
        v.image = "result_succ".toImage
        v.contentMode = .scaleAspectFill
        return v
    }()

    private let successTitleLabel: UILabel = {
        let label = UILabel()
        label.text = "Merge Successfully!"
        label.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        label.textColor = .black
        return label
    }()

    private let thumbnailImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.layer.cornerRadius = 16
        return imageView
    }()
    
    private let fileNameLabel: UILabel = {
        let label = UILabel()
        label.text = "12345798CSACBSJCUIS.PDF"
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        label.textColor = .black
        return label
    }()
    
    private let fileDateSizeLabel: UILabel = {
        let label = UILabel()
        label.text = "2024-09-14 24 MB"
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = "#ADADAD".toColor
        return label
    }()
    
    private let filePathLabel: UILabel = {
        let label = UILabel()
        label.text = "/DATA/USER/0/COM.PDFVIEWER.SCANNER/FILES/DEMO/DEMO.DOCX"
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = "#ADADAD".toColor
        label.numberOfLines = 3
        return label
    }()
    
    private let openButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("Open", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        button.backgroundColor = "#333FFF".toColor
        button.layer.cornerRadius = 20
        return button
    }()
    
    private let shareButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("Share", for: .normal)
        button.setTitleColor("#333FFF".toColor, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .bold)
        button.backgroundColor = "#DADCFF".toColor
        button.layer.cornerRadius = 20
        return button
    }()
    
    private let lineView: UIImageView = {
        let v = UIImageView()
        v.image = "result_line".toImage
        v.contentMode = .scaleAspectFill
        return v
    }()
    
    // MARK: - Initialization
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        setupConstraints()
        setupActions()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Setup
    
    private func setupUI() {
        backgroundColor = .white
        
        addSubview(successIconView)
        addSubview(successTitleLabel)
        
        addSubview(thumbnailImageView)
        addSubview(fileNameLabel)
        addSubview(fileDateSizeLabel)
        addSubview(filePathLabel)
        
        addSubview(openButton)
        addSubview(shareButton)
        
        addSubview(lineView)
    }
    
    private func setupConstraints() {
        
        let w = (w_screen - 42)/2
        
        successIconView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(85)
            make.top.equalToSuperview().offset(10)
            make.width.height.equalTo(50)
        }

        successTitleLabel.snp.makeConstraints { make in
            make.leading.equalTo(successIconView.snp.trailing).offset(4)
            make.centerY.equalTo(successIconView)
        }

        thumbnailImageView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(15)
            make.top.equalTo(successIconView.snp.bottom).offset(24)
            make.width.equalTo(86)
            make.height.equalTo(120)
        }
        
        fileNameLabel.snp.makeConstraints { make in
            make.leading.equalTo(thumbnailImageView.snp.trailing).offset(12)
            make.trailing.equalToSuperview().offset(-16)
            make.top.equalTo(thumbnailImageView).offset(22)
            make.height.equalTo(16)
        }
        
        fileDateSizeLabel.snp.makeConstraints { make in
            make.leading.trailing.equalTo(fileNameLabel)
            make.top.equalTo(fileNameLabel.snp.bottom).offset(6)
            make.height.equalTo(14)
        }
        
        filePathLabel.snp.makeConstraints { make in
            make.leading.trailing.equalTo(fileNameLabel)
            make.top.equalTo(fileDateSizeLabel.snp.bottom).offset(12)
        }
        
        openButton.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(15)
            make.top.equalTo(thumbnailImageView.snp.bottom).offset(20)
            make.height.equalTo(40)
            make.width.equalTo(w)
        }
        
        shareButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-15)
            make.top.height.width.equalTo(openButton)
        }
        
        lineView.snp.makeConstraints { make in
            make.trailing.leading.equalToSuperview().inset(15)
            make.height.equalTo(1)
            make.top.equalTo(openButton.snp.bottom).offset(20)
        }
    }
    
    private func setupActions() {
        openButton.addTarget(self, action: #selector(openButtonTapped), for: .touchUpInside)
        shareButton.addTarget(self, action: #selector(shareButtonTapped), for: .touchUpInside)
    }
    
    // MARK: - Actions
    
    @objc private func openButtonTapped() {
        onOpenTapped?()
    }
    
    @objc private func shareButtonTapped() {
        onShareTapped?()
    }
    
    // MARK: - Public Methods
    
    enum OperationType {
        case merge
        case split
        
        var successTitle: String {
            switch self {
            case .merge:
                return "Merge Successfully!"
            case .split:
                return "Split Successfully!"
            }
        }
    }
    
    func configure(fileName: String, date: String, size: String, filePath: String, thumbnail: UIImage? = nil, operationType: OperationType = .merge) {
        successTitleLabel.text = operationType.successTitle
        fileNameLabel.text = fileName
        fileDateSizeLabel.text = "\(date) \(size)"
        filePathLabel.text = filePath
        if let thumbnail = thumbnail {
            thumbnailImageView.image = thumbnail
            thumbnailImageView.contentMode = .scaleAspectFill
        }
    }
}
